package com.hanoi.wiki.dao.hibernate;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.springframework.orm.jpa.JpaCallback;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hanoi.wiki.dao.UserStatisticDAO;
import com.hanoi.wiki.exception.DataAccessException;
import com.hanoi.wiki.model.UserStatistic;

@Component
@Transactional
public class UserStatisticDAOHibernate extends HibernateBaseDAO<UserStatistic, Integer> implements UserStatisticDAO{

	public UserStatisticDAOHibernate(Class<UserStatistic> persistenceClass) {
		super(persistenceClass);
		// TODO Auto-generated constructor stub
	}
	
	public UserStatisticDAOHibernate() {
		super(UserStatistic.class);
	}

	@SuppressWarnings("rawtypes")
	@Override
	protected com.hanoi.wiki.dao.hibernate.HibernateBaseDAO.QueryObject buildQueryObject(
			String selectClause, UserStatistic model) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<UserStatistic> getList() throws DataAccessException {
		StringBuilder strBuilder = new StringBuilder();
		strBuilder.append("SELECT us FROM UserStatistic us");
		final String queryStr = strBuilder.toString();
		
		@SuppressWarnings("unchecked")
		List<UserStatistic> results = (List<UserStatistic>)getJpaTemplate().execute(new JpaCallback<Object>() {
			public Object doInJpa(EntityManager em) throws PersistenceException {
				final Query q = em.createQuery(queryStr);
				return q.getResultList();
			}
		});
		
		return results;
	}

	public int saveUserStatistic(UserStatistic statistic)
			throws DataAccessException {
		save(statistic);
		return statistic.getId();
	}

}
